/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of GL2x.

    GL2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    GL2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GL2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdlib.h>

#include "driver/driver.h"

struct gltexture* cache1d[256];
struct gltexture* cache2d[256];
struct gltexture* cache3d[256];

void GL2x_initTexCache() {
    int i;

    for(i = 0; i < 256; i++) {
        cache1d[i] = 0;
        cache2d[i] = 0;
        cache3d[i] = 0;
    }
}

void GL2x_emptyTexCache() {
    int i;

    for(i = 0; i < 256; i++) {
        if(cache1d[i]) {
            free(cache1d[i]->pixels);
            free(cache1d[i]);
        }
        if(cache2d[i]) {
            free(cache2d[i]->pixels);
            free(cache2d[i]);
        }
        if(cache3d[i]) {
            free(cache3d[i]->pixels);
            free(cache3d[i]);
        }
    }
}

void GL2x_setTexture(struct gltexture* tex) {
    switch(tex->dim) {
    case 1:
        if(cache1d[bound_tex_1d]) {
            free(cache1d[bound_tex_1d]->pixels);
            free(cache1d[bound_tex_1d]);
        }
        cache1d[bound_tex_1d] = tex;
    case 2:
        if(cache2d[bound_tex_2d]) {
            free(cache2d[bound_tex_2d]->pixels);
            free(cache2d[bound_tex_2d]);
        }
        cache2d[bound_tex_2d] = tex;
    case 3:
        if(cache3d[bound_tex_3d]) {
            free(cache3d[bound_tex_3d]->pixels);
            free(cache3d[bound_tex_3d]);
        }
        cache1d[bound_tex_3d] = tex;
    }
}
